const query = require('../models');

module.exports = {
    get: function (req, res) {
        const { current, pageSize } = req.query;
        const start = (Number(current) - 1) * Number(pageSize);
        const sql = `
      SELECT book_name bookName,bookId bookId,book_subject bookSubject,book_author bookAuthor,book_publish bookPublish,book_price bookPrice,book_position bookPosition,book_time bookTime
FROM books where is_del = 1 limit ${start},${pageSize}`;
        query(sql,null)
            .then(function (doc){
                res.json({
                    code: '200',
                    msg: '查询成功',
                    data: doc
                });
            })
            .catch(function (err){
                res.json({
                    code: '1002',
                    msg: '查询失败',
                    err
                });
            })
    },
    del: function (req,res) {
        const { idArr } = req.body;
        const sql = `
            UPDATE books SET is_del = 0 WHERE bookId = ${idArr};
        `;
        query(sql,null)
            .then(function (doc){
                res.json({
                    code: '200',
                    msg: '查询成功',
                    data: doc
                });
            })
            .catch(function (err){
                res.json({
                    code: '1002',
                    msg: '查询失败',
                    err
                });
            })
    },
    add: function (req,res) {
        const {name, subject, author, publish, price, position, time} = req.body;
        const sql = `
            INSERT INTO books(book_name, book_subject, book_author, book_publish, book_price, book_position, book_time, is_del) VALUES(${name}, ${subject}, ${author}, ${publish}, ${price}, ${position}, ${time}, 1)
        `;
        query(sql, null).then(function () {
                res.json({
                    code: '200',
                    msg: '操作成功'
                });
            })
            .catch(function (err) {
                res.json({
                    code: '1002',
                    msg: '操作失败',
                    err
                });
            });
    },
    edit: function (req,res) {
        console.log('编辑请求参数：', req.body);
        const {id, name, subject, author, publish, price, position, time} = req.body;
        const sql = `
            UPDATE books SET book_name=${name},book_subject=${subject},book_author=${author},book_publish=${publish},book_price=${price},book_position=${position},book_time=${time} WHERE bookId = ${id};
        `
        query(sql, [id, name, subject, author, publish, price, position, time])
            .then(function (result) {
                console.log('编辑之后的结果：', result);
                res.json({
                    code: '200',
                    msg: '操作成功'
                });
            })
            .catch(function (err) {
                res.json({
                    code: '1002',
                    msg: '操作失败',
                    err
                });
            });
    },
    //查看
    getById: function (req,res) {
        const {id} = req.body;
        const sql = `
            SELECT * FROM books where bookId = ${id}
        `;
        query(sql,null)
            .then(function (doc){
                res.json({
                    code: '200',
                    msg: '查询成功',
                    data: doc
                });
            })
            .catch(function (err){
                res.json({
                    code: '1002',
                    msg: '查询失败',
                    err
                });
            })
    }


}
